package cn.edu.zut.hhr.controller;


import cn.edu.zut.hhr.entity.Commodity;
import cn.edu.zut.hhr.entity.CommodityInfo;
import cn.edu.zut.hhr.entity.Order;
import cn.edu.zut.hhr.entity.OrderInfo;
import cn.edu.zut.hhr.exceptions.CheckException;
import cn.edu.zut.hhr.mapper.OrderInfoMapper;
import cn.edu.zut.hhr.service.IOrderService;
import cn.edu.zut.hhr.utils.CheckUtil;
import cn.edu.zut.hhr.utils.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhr
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    IOrderService orderService;
    @Resource(name = "batchSqlSession")
    SqlSession sqlSession;

    @GetMapping("/id/{id}")
    public RestResponse<Order> getOrderById(@PathVariable("id") Long id){
        Order order = orderService.getByOrderId(id);
        if(order == null){
            return RestResponse.fail("不存在该订单");
        }
        return RestResponse.success(order);
    }

    @GetMapping("/customerid/{customerid}")
    public RestResponse<List<Order>> getOrderByCustomerid(@PathVariable(value = "customerid", required = false) Long customerid,
                                                   HttpSession session){
        if (customerid == null){
            customerid = Long.valueOf(session.getAttribute("userid").toString());
        }
        List<Order> orders = orderService.getOrderByCustomerid(customerid);
        return RestResponse.success(orders);
    }

    @PostMapping("/")
    public RestResponse<Order> insertOrder(@RequestBody Order order,
                                           HttpSession session){
        if(order.getCustomerid() == null){
            order.setCustomerid(Long.valueOf(session.getAttribute("userid").toString()));
        }
        if(order.getCustomerName() == null){
            order.setCustomerName(session.getAttribute("username").toString());
        }
        if(order.getStaffid() == null){
            order.setStaffid(-1L);
            order.setStaffName("外送订单无处理人员");
        } else{
            CheckUtil.notNull(order.getStaffName(),"处理人员名称不能为空");
        }
        orderService.save(order);
        //顺序不能换
        setOrderInfo(order.getId(), order.getOrderInfos());
        return RestResponse.success(order);
    }

    private void setOrderInfo(Long oid, List<OrderInfo> orderInfos){
        OrderInfoMapper orderInfoMapper = sqlSession.getMapper(OrderInfoMapper.class);
        for(int i = 0; i < orderInfos.size(); i++){
            OrderInfo orderInfo = orderInfos.get(i);
            orderInfo.setOid(oid);
            orderInfoMapper.insert(orderInfo);
        }
        sqlSession.commit();
    }

}
